<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="mybatis.mappers.CrawlerMapper">
    <update id="updateSession">
        update crawler_sessions
        set
        siteInfo = #{siteInfo}
        where sessionId = #{sessionId};
    </update>

    <select id="getAllCrawlers" resultType="models.entities.CrawlerEntity">
        select * from crawlers;
    </select>
    <select id="getCrawlerInfo" resultType="models.entities.CrawlerEntity">
        select * from crawlers where title = #{crawlerName};
    </select>
    <select id="getSessions" resultType="models.crawlers.common.SessionParams">
        select * from crawler_sessions where crawlerName = #{crawlerTitle};
    </select>
    <select id="getSession" resultType="models.crawlers.common.SessionParams">
        select * from crawler_sessions where sessionId = #{sessionId};
    </select>
    <insert id="writeAction" parameterType="models.entities.CrawlerActionEntity">
        insert into crawler_session_actions (crawlerAction, link, entityId, sessionId)
        values(#{crawlerAction}, #{link}, #{entityId}, #{sessionId});
    </insert>
    <insert id="writeWarning" parameterType="models.entities.CrawlerWarningEntity">
        insert into crawler_session_warnings (crawlerWarning, entityId, sessionId, warningText)
        values(#{crawlerWarning}, #{entityId}, #{sessionId}, #{warningText});
    </insert>
    <update id="updateCrawlerInfo">
        <![CDATA[
        update crawlers
            set webSiteInfo = #{webSiteInfo},
                status = #{status},
                updatedDate = CURRENT_TIMESTAMP,
                productIndex = #{productIndex}
        where title = #{title};
        update crawlers
            set productsCount = #{productsCount}
        where title = #{title} and productsCount < #{productsCount};
        update crawlers
            set categoriesCount = #{categoriesCount}
            where title = #{title} and categoriesCount < #{categoriesCount};
          ]]>
    </update>
    <update id="setProductCount">
        update crawlers
        set productsCount = #{productsCount},
        categoriesCount = #{categoriesCount}
        where title = #{title};
    </update>
    <update id="changePageNumber">
        update crawler_sessions
        set pageNumber = #{pageNumber}
        where sessionId = #{sessionId};
    </update>
    <update id="changeCategoryIndex">
        update crawler_sessions
        set categoryIndex = #{categoryIndex}
        where sessionId = #{sessionId};
    </update>
</mapper>